<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redis6.0%E6%96%B0%E7%89%B9%E6%80%A7-io%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%85%B6%E4%BA%8C/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">372 字 ~2分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<blockquote>
<p><a href="https://mp.weixin.qq.com/s/ecilhMZE6ZK9G_icybnEdQ">https://mp.weixin.qq.com/s/ecilhMZE6ZK9G_icybnEdQ</a></p>
</blockquote>

<h1 id="redis6-0新特性-thread-io多线程">Redis6.0新特性-Thread/IO多线程</h1>

<h2 id="一-背景">一、背景</h2>

<p>目前快手有70w+的Redis实例，在线上的Redis集群，我们经常会碰到以下的一些情况：</p>

<p>（1） 由于键值设计不合理或者业务特性导致的热点问题（集群整体QPS不高，但是集群内某个实例的请求特别高），严重影响业务侧请求的返回时间</p>

<p>（2） 集群内某个实例直连集群连接数过多，单线程模型处理缓慢，影响其他的请求</p>

<p>（3） 集群内某个实例网络不稳定后者pipeline个数较多，导致协议解析频繁调用，导致cpu时间占用过长，影响其他的客户端请求</p>

<p>以上这些问题，相信大家也都碰到过，那么这些问题与Redis的单线程模型又有什么关系？</p>

<h3 id="1-为什么redis6之前是单线程设计">1. 为什么Redis6之前是单线程设计？</h3>

<p>首先，我们明确一点，Redis6之前的Redis4，Redis5并不是单线程程序。通常我们说的Redis的单线程，是指Redis接受链接，接收数据并解析协议，发送结果等命令的执行，都是在主线程中执行的。</p>

<p>Redis之前之所以将这些都放在主线程中执行，主要有以下几方面的原因：</p>

<ul>
<li>Redis的主要瓶颈不在cpu，而在内存和网络IO</li>
<li>使用单线程设计，可以简化数据库结构的设计</li>
<li>可以减少多线程锁带来的性能损耗</li>
</ul>

<h3 id="2-什么是io多线程">2. 什么是IO多线程？</h3>

<p>既然Redis的主要瓶颈不在CPU，为什么又要引入IO多线程？Redis的整体处理流程如下图：</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220701110031.png" width = 600 /> </div>

<p>结合上图可知，当 socket 中有数据时，Redis 会通过系统调用将数据从内核态拷贝到用户态，供 Redis 解析用。这个拷贝过程是阻塞的，术语称作 “同步阻塞IO”，数据量越大拷贝的延迟越高，解析协议时间消耗也越大，糟糕的是这些操作都是在主线程中处理的，特别是链接数特别多的情况下，这种情况更加明显。基于以上原因，Redis作者提出了Thread/IO线程，既将接收与发送数据来使用多线程并行处理，从而降低主线程的等待时间。</p>

<h2 id="二-thread-io整体流程及程序实现设计">二、Thread/IO整体流程及程序实现设计</h2>

<h3 id="1-thread-io整体实现思路">1.Thread/IO整体实现思路</h3>

<p>(1).创建一组大小为io线程个数的等待队列，用来存储客户端的网络套接字。</p>

<p>(2).分均分配客户端网络套接字到等待队列中</p>

<p>(3).等待线程组接收解协议完毕或者发送数据完毕</p>

<p>(4).执行后续操作，然后跳转到第2步继续执行</p>

<h3 id="2-thread-io涉及到的代码文件">2.Thread/IO涉及到的代码文件</h3>

<p>关于IO多线程部分的代码，在src/network.c中。</p>

<h3 id="3-thread-io整体流程图">3.Thread/IO整体流程图</h3>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220701110140.png" width = 900 /> </div>

<h2 id="三-关键代码流程详解">三、关键代码流程详解</h2>

<h3 id="1-io线程的初始化">1.io线程的初始化</h3>

<p>Redis多线程相关线程的初始化顺序</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220701110249.png" width = 500 /> </div>

<p>初始化相关流程图及代码创建线程流程图</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220701110307.png" width = 900 /> </div>

<p>创建线程代码具体位置</p>

<ul>
<li><p><code>src/network.c: initThreadIO(void)</code></p></li>

<li><p><code>src/network.c: IOThreadMain(void *)</code></p></li>
</ul>

<h3 id="2-readqueryfromclient部分">2.readQueryFromClient部分</h3>

<p>代码具体位置：<code>src/network.c: readQueryFromClient(connection *)</code></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-C" data-lang="C"><span style="color:#204a87;font-weight:bold">void</span> <span style="color:#000">readQueryFromClient</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">connection</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">conn</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#000">client</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">c</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">connGetPrivateData</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">conn</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">nread</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">readlen</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000">size_t</span> <span style="color:#000">qblen</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#8f5902;font-style:italic">/* Check if we want to read from the client later when exiting from
</span><span style="color:#8f5902;font-style:italic">     * the event loop. This is the case if threaded I/O is enabled. */</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">postponeClientRead</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#204a87;font-weight:bold">return</span><span style="color:#000;font-weight:bold">;</span>

  <span style="color:#ce5c00;font-weight:bold">/</span>

   <span style="color:#8f5902;font-style:italic">/* 单线程解析部分代码 */</span>
   <span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000;font-weight:bold">.</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div><div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-C" data-lang="C"><span style="color:#204a87;font-weight:bold">int</span> <span style="color:#000">postponeClientRead</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">client</span> <span style="color:#ce5c00;font-weight:bold">*</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
    <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">io_threads_active</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#ce5c00;font-weight:bold">&amp;</span>
        <span style="color:#000">server</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">io_threads_do_reads</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#ce5c00;font-weight:bold">&amp;</span>
        <span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#000">ProcessingEventsWhileBlocked</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span><span style="color:#ce5c00;font-weight:bold">&amp;</span>
        <span style="color:#ce5c00;font-weight:bold">!</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">c</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">flags</span> <span style="color:#ce5c00;font-weight:bold">&amp;</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">CLIENT_MASTER</span><span style="color:#ce5c00;font-weight:bold">|</span><span style="color:#000">CLIENT_SLAVE</span><span style="color:#ce5c00;font-weight:bold">|</span><span style="color:#000">CLIENT_PENDING_READ</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span>
    <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">c</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">flags</span> <span style="color:#ce5c00;font-weight:bold">|</span><span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">CLIENT_PENDING_READ</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">listAddNodeHead</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">server</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">clients_pending_read</span><span style="color:#000;font-weight:bold">,</span><span style="color:#000">c</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">1</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">else</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
<span style="color:#000;font-weight:bold">}</span></code></pre></div>
<p>由上面部分的代码，可以得知客户端要使用io多线程必须满足的条件有：</p>

<ol>
<li><p>io多线程已经激活</p></li>

<li><p>io多线程允许read</p></li>

<li><p>没有阻塞的处理事件</p></li>

<li><p>客户端不是主节点、从节点、已分配的延迟接收客户端</p></li>
</ol>

<h3 id="3-处理读取待分配任务">3.处理读取待分配任务</h3>

<p>处理读取待分配任务流程图</p>

<p><img src="https://mmbiz.qpic.cn/mmbiz_png/ZULLb8I10cyPZuk5S75AWru8Zt8iaYAdiaF8aXFuZH29ETwxzo0Nhiae9hdvJ3Pzhibp0giaSB0icpicWF36ibXeoUQ3DA/640?wx_fmt=png&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片" /></p>

<p>处理读取待分配任务代码</p>

<p><code>src/networking.c: handleClientsWithPendingReadsUsingThreads(void)</code></p>

<h3 id="4-处理写待分配任务">4.处理写待分配任务</h3>

<p>写分配任务具体流程同读分配流程大同小异，看参照上图阅读具体的代码实现，代码位置：</p>

<p><code>src/networking.c: handleClientsWithPendingWritesUsingThreads(void)</code></p>

<h3 id="5-io线程的动态开与关">5.IO线程的动态开与关</h3>

<p>主线程中整体读与写的逻辑流程图</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220701110355.png" width = 900 /> </div>

<p>​   具体代码位置：<code>src/server.c: beforSleep(struct aeEventLoop *)</code></p>

<p>IO线程的开关循序流程图</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220701110436.png" width = 900 /> </div>

<p>由上图可知，IO线程的开关，实际取决于延迟发送客户端的数量是否小于IO线程数*2，否则IO线程一般都处于阻塞状态（即使设置了IO多线程）。</p>

<p>具体代码位置:</p>

<ul>
<li><p><code>src/networking.c: handleClientsWithPendingWritesUsingThreads(void)</code></p></li>

<li><p><code>src/networking.c: int stopThreadedIOIfNeeded(void)</code></p></li>

<li><p><code>src/networking.c: startThreadedIO(void)</code></p></li>
</ul>

<h2 id="四-配置文件对应配置项说明">四、配置文件对应配置项说明</h2>

<table>
<thead>
<tr>
<th>配置项</th>
<th>说明</th>
<th>默认值</th>
<th>备注</th>
</tr>
</thead>

<tbody>
<tr>
<td>io-threads</td>
<td>配置redis的io线程个数</td>
<td>4</td>
<td>最大不超过128，且默认不开启</td>
</tr>

<tr>
<td>io-threads-do-reads</td>
<td>io线程负责read</td>
<td>no</td>
<td>默认io线程只负责发送数据，既wirte数据返回给客户端</td>
</tr>
</tbody>
</table>

<h2 id="五-thread-io性能测试">五、Thread/IO性能测试</h2>

<h3 id="1-测试方法与测试指标">1.测试方法与测试指标</h3>

<p>本次测试，采用vire-benchmark工具进行压测。redis-server与vire-benchmark对应压测参数如下：</p>

<h3 id="1-redis-server">（1）redis-server</h3>

<table>
<thead>
<tr>
<th>配置</th>
<th>说明</th>
<th>值</th>
</tr>
</thead>

<tbody>
<tr>
<td>io-threads</td>
<td>io线程的数量</td>
<td>[1,2,3,4,5,6,7,8]</td>
</tr>

<tr>
<td>io-threads-do-reads</td>
<td>io线程是否负责接收数据</td>
<td>yes</td>
</tr>
</tbody>
</table>

<h3 id="2-vire-benchmark">（2） vire-benchmark</h3>

<table>
<thead>
<tr>
<th>压测项</th>
<th>值</th>
</tr>
</thead>

<tbody>
<tr>
<td>客户端数量</td>
<td>[1,10,50]</td>
</tr>

<tr>
<td>payload大小</td>
<td>[20,64,256,1024,2048,8192]</td>
</tr>

<tr>
<td>pipeline个数</td>
<td>[10,50,100,500]</td>
</tr>

<tr>
<td>压测命令</td>
<td>get,set,mget_10,mget_100,mset,lpush,rpush,lpop,rpop,lrange_10,lrange_100,sadd,spop,hset, hincrby,hget,hmset,hmget,hgetall,zadd,zrem,pfadd,pfcount</td>
</tr>
</tbody>
</table>

<h3 id="2-测试结果分析">2. 测试结果分析</h3>

<h3 id="1-各个命令的极限qps">(1) 各个命令的极限QPS</h3>

<p>注：压测结果不考虑客户端数量，payload大小，pipeline个数，只是直观展现Redis6开启io多线程后，针对各个命令所能达到的极限QPS。</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220701110848.png" width = 900 /> </div>

<h3 id="2-io线程数量与极限qps的关系">(2) IO线程数量与极限QPS的关系</h3>

<p>在极限QPS的测试结果中，client个数基本上都为1，不具备实际意义，再结合线上实际业务情况，选取GET，client_count(50)，payload（20），pipeline（50）作为结果分析的参考基准。</p>

<p>Get，client_count（50），payload（20），pipeline（50）</p>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220701111114.png" width = 600 /> </div>

<p>由上图可知，针对Get等简单命令，IO线程也并不是越多越好，这也符合代码中的自旋锁的实现逻辑。但是IO多线程，确实能够再一定程度上提升QPS，但是效果并不是很明显。</p>

<p>Get，client_count（50），pipeline（50）</p>

<table>
<thead>
<tr>
<th><strong>描述</strong></th>
<th><strong>1</strong></th>
<th><strong>2</strong></th>
<th><strong>3</strong></th>
<th><strong>4</strong></th>
<th><strong>5</strong></th>
<th><strong>6</strong></th>
<th><strong>7</strong></th>
<th><strong>8</strong></th>
</tr>
</thead>

<tbody>
<tr>
<td><strong>payload(20)</strong></td>
<td>1234567</td>
<td>1282051</td>
<td>1315789</td>
<td>1562499</td>
<td>1204819</td>
<td>1408450</td>
<td>1265822</td>
<td>1282051</td>
</tr>

<tr>
<td><strong>payload(64)</strong></td>
<td>1250000</td>
<td>1234567</td>
<td>1315789</td>
<td>1333333</td>
<td>1176470</td>
<td>1388889</td>
<td>1333333</td>
<td>1428571</td>
</tr>

<tr>
<td><strong>payload(256)</strong></td>
<td>1123595</td>
<td>970873</td>
<td>1190476</td>
<td>1351351</td>
<td>1369863</td>
<td>1298701</td>
<td>1369863</td>
<td>1333333</td>
</tr>

<tr>
<td><strong>payload(1024)</strong></td>
<td>694444</td>
<td>806451</td>
<td>757575</td>
<td>793650</td>
<td>847457</td>
<td>833333</td>
<td>900900</td>
<td>862069</td>
</tr>

<tr>
<td><strong>payload(2048)</strong></td>
<td>431034</td>
<td>357142</td>
<td>381679</td>
<td>400000</td>
<td>446428</td>
<td>442477</td>
<td>485436</td>
<td>467289</td>
</tr>

<tr>
<td><strong>payload(4096)</strong></td>
<td>218818</td>
<td>232558</td>
<td>264550</td>
<td>247524</td>
<td>255754</td>
<td>237529</td>
<td>239808</td>
<td>265252</td>
</tr>
</tbody>
</table>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220701111206.png" width = 900 /> </div>

<p>由上图可知，IO多线程对payload较大请求，处理效果提升明显，但是当payload提高到2048左右时，提升效果不明显。</p>

<p>Get，pipeline（50），bytes（20）</p>

<table>
<thead>
<tr>
<th><strong>描述</strong></th>
<th><strong>1</strong></th>
<th><strong>2</strong></th>
<th><strong>3</strong></th>
<th><strong>4</strong></th>
<th><strong>5</strong></th>
<th><strong>6</strong></th>
<th><strong>7</strong></th>
<th><strong>8</strong></th>
</tr>
</thead>

<tbody>
<tr>
<td><strong>client(1)</strong></td>
<td>1408450</td>
<td>1388889</td>
<td>1449275</td>
<td>1470588</td>
<td>1515151</td>
<td>1515151</td>
<td>1538461</td>
<td>1492537</td>
</tr>

<tr>
<td><strong>client(20)</strong></td>
<td>1408450</td>
<td>1408450</td>
<td>1315789</td>
<td>1351351</td>
<td>1515151</td>
<td>1449275</td>
<td>1538461</td>
<td>1449275</td>
</tr>

<tr>
<td><strong>client(50)</strong></td>
<td>1234567</td>
<td>1282051</td>
<td>1315789</td>
<td>1562499</td>
<td>1204819</td>
<td>1408450</td>
<td>1265822</td>
<td>1282051</td>
</tr>
</tbody>
</table>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220701111254.png" width = 900 /> </div>

<p>对于客户端数量而言，IO多线程针对多个客户端有一定提升，但是效果不是很明显。上图也可观察到，当客户端数量提高到50左右时，IO线程越多，QPS反而成下降趋势。</p>

<p>Get，client_count（50），payload（20）</p>

<table>
<thead>
<tr>
<th><strong>描述</strong></th>
<th><strong>1</strong></th>
<th><strong>2</strong></th>
<th><strong>3</strong></th>
<th><strong>4</strong></th>
<th><strong>5</strong></th>
<th><strong>6</strong></th>
<th><strong>7</strong></th>
<th><strong>8</strong></th>
</tr>
</thead>

<tbody>
<tr>
<td><strong>pipeline(10)</strong></td>
<td>757575</td>
<td>869565</td>
<td>1010101</td>
<td>934579</td>
<td>917431</td>
<td>1123595</td>
<td>1010101</td>
<td>1052631</td>
</tr>

<tr>
<td><strong>pipeline(50)</strong></td>
<td>1234567</td>
<td>1282051</td>
<td>1315789</td>
<td>1562499</td>
<td>1204819</td>
<td>1408450</td>
<td>1265822</td>
<td>1282051</td>
</tr>

<tr>
<td><strong>pipeline(100)</strong></td>
<td>1470588</td>
<td>1298701</td>
<td>1369863</td>
<td>1408450</td>
<td>1250000</td>
<td>1369863</td>
<td>1428571</td>
<td>1298701</td>
</tr>
</tbody>
</table>

<div  align="center"><img src="https://wejack639.oss-cn-beijing.aliyuncs.com/blogimages/img/20220701111316.png" width = 900 /> </div>

<p>由上图可知，在pipeline个数较少时，qps随着IO线程数量增多缓慢增加，当pipeline个数大于50个以上，随着线程数的增加，QPS成缓慢下降趋势。</p>

<h3 id="3-测试结果总结">3. 测试结果总结</h3>

<ul>
<li><p>IO线程总体提升效果并不是非常明显，以基准结果为例，当IO线程为4时，较单线程在同条件下，QPS提升25%左右</p></li>

<li><p>IO线程，对val与pipeline较大的操作，有明显的提升，当IO线程为4时，较单线程在同条件下，QPS最大提升30%左右，但随着val的增大，提升并不明显</p></li>

<li><p>对于多个客户端，IO多线程的提升并不明显</p></li>

<li><p>通过测试数据，发现Redis的IO多线程，在设置为4个时可达到最佳效率，到达最高QPS</p></li>
</ul>

<h2 id="六-使用thread-io注意事项">六、使用Thread/IO注意事项</h2>

<ul>
<li><p>io-threads个数设置为1时，实际上还是只使用主线程</p></li>

<li><p>io-threads默认只负责write，既发送数据给客户端</p></li>

<li><p>io-threads的个数一旦设置，不能通过config动态设置</p></li>

<li><p>当设置ssl后，io-threads将不工作</p></li>

<li><p>通常io线程并不能有太大提升，除非cpu占用特别明显或者客户端链接特别多的情况下，否则不建议使用</p></li>

<li><p>io线程只能是读，或者写，不存在读写并存的情况（这点配置文档上没写，不知道作者是如何得出的结论，需要 参考源码看看）</p></li>

<li><p>Io线程的开关，实际取决于延迟发送客户端的数量是否小于IO线程数*2</p></li>
</ul>

<h2 id="七-线上当前redis6的情况">七、线上当前Redis6的情况</h2>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">注：KCC是快手内部Redis管控平台，目前管理70万Redis、Memcache实例，3000+ElasticSearch节点。由于实例数量庞大，热点问题不可避免，为此上Redis 6也是刚需。</code></pre></div>
<p>1.KCC线上目前已上线30+，集群运行稳定，如需申请，在KCC平台请注明使用Redis6版本即可</p>

<p>2.改造代理内核源码，覆盖Redis5/6新增的全部命令，如：bitfield_ro、stralgo、zpopmin、zpopmax等</p>

<p>3.改造Redis 6升级工具，支持持Redis3、4、5、6任一版本升降。</p>

<p>4.由于键值设计不合理以及业务特性带来的热点问题，Redis单线程版本单个实例达到请求瓶颈，切换到Redis6版本后，单实例QPS可到20~30W+，效果符合预期，降低了业务高负载时的请求延时问题</p>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis6.0%E6%96%B0%E7%89%B9%E6%80%A7-io%E5%A4%9A%E7%BA%BF%E7%A8%8B%E5%85%B6%E4%B8%89/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis6.0%E6%96%B0%E7%89%B9%E6%80%A7-tls/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>